package com.baozun.houji.me.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.adapter.MyCollectChannelAdapter
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseListFragment
import com.baozhun.mall.common.databinding.FragmentBaseListBinding
import com.baozhun.mall.common.databinding.ItemCollectionChannelBinding
import com.baozhun.mall.common.ext.loadArrayListData
import com.baozhun.mall.common.model.bean.GoodsInfoBean
import com.baozhun.mall.common.model.enums.GoodsType
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.widget.EmptyView
import com.baozun.houji.me.R
import com.baozun.houji.me.viewmodel.CollectionViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.DraggableModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * @Title：我的收藏activity
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/12
 * @Version：1.0.0
 */
class MyCollectionChannelFragment : BaseListFragment<CollectionViewModel, FragmentBaseListBinding, ItemCollectionChannelBinding, GoodsInfoBean>(), DraggableModule {

    private val mCollectionListAdapter by lazy { MyCollectChannelAdapter(mutableListOf(), this) }

    companion object {
        fun newInstance(): MyCollectionChannelFragment {
            val args = Bundle()
            val fragment = MyCollectionChannelFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mCollectionListAdapter.addChildClickViewIds(com.baozhun.mall.common.R.id.tv_delete)
        mCollectionListAdapter.setEmptyView(
            EmptyView.Builder(mActivity).emptyHintText(com.baozhun.mall.res.R.string.no_collect_data_hint).emptyImage(R.drawable.ic_data_empty_colloect).build()
        )
    }

    override fun createAdapter(): BaseQuickAdapter<GoodsInfoBean, BaseDataBindingHolder<ItemCollectionChannelBinding>>? {
        return mCollectionListAdapter
    }

    override fun loadData(isRefresh: Boolean) {
        mViewModel.getCollectionList(isRefresh, GoodsType.CHANNEL.type)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.mCollectionList.observe(this) {
            loadArrayListData(it, mCollectionListAdapter, mDataBinding.baseRefreshRv)
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        ARouter.getInstance().build(RoutePath.Home.GOODS_DETAIL).withString(Constants.Goods.GOODS_ID, mCollectionListAdapter.getItem(position).goods_id)
            .withSerializable(Constants.Goods.GOODS_SOURCE, mCollectionListAdapter.getItem(position).source).navigation()
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        var item = mCollectionListAdapter.getItem(position)
        when (view.id) {
            com.baozhun.mall.common.R.id.tv_delete -> {
                mViewModel.deleteCollect(item.goods_id, GoodsType.CHANNEL.type) {
                    mCollectionListAdapter.removeAt(position)
                }
            }
        }
    }

    override fun useFragmentViewModelStoreOwner(): Boolean {
        return true
    }
}